package com.wannengbang.storemobile.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.BankDeposiAddressListBean;
import com.wannengbang.storemobile.homepage.adapter.AddressAdapter;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.homepage.model.IHomePageModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author:HWC
 */
public class AddressDialog extends DialogFragment {


    @BindView(R.id.tv_provience_name)
    TextView tvProvienceName;
    @BindView(R.id.view_provience)
    View viewProvience;
    @BindView(R.id.ll_provience)
    LinearLayout llProvience;

    @BindView(R.id.tv_city_name)
    TextView tvCityName;
    @BindView(R.id.view_city)
    View viewCity;
    @BindView(R.id.ll_city)
    LinearLayout llCity;

    @BindView(R.id.tv_area_name)
    TextView tvAreaName;
    @BindView(R.id.view_area)
    View viewArea;
    @BindView(R.id.ll_area)
    LinearLayout llArea;

    @BindView(R.id.rv_address)
    RecyclerView rv_address;

    private Dialog dialog;
    private OnCallBackListener onCallBackListener;
    private Activity mActivity;

    private IHomePageModel homePageModel = new HomePageModelImpl();
    private AddressAdapter provienceAdapter;
    private List<BankDeposiAddressListBean.DataBean> addressList = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_address, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        initData();
        setListener();
        return view;
    }


    int code = 0000;
    int level = 1;

    String provinceName = "";
    String provinceCode = "";
    String cityName = "";
    String cityCode = "";
    String areaName = "";
    String areaCode = "";

    private String bank_name = "";
    private String bank_type = "";

    private void initData() {
        requestGetRegion(code, level);
    }

    private void setListener() {
        provienceAdapter = new AddressAdapter(addressList, 1);

        rv_address.setLayoutManager(new LinearLayoutManager(mActivity));
        rv_address.setAdapter(provienceAdapter);


        provienceAdapter.setOnCallBackListener(new AddressAdapter.OnCallBackListener() {
            @Override
            public void onCallBack(String name, String code) {


                if (level == 1) {
                    provinceName = name;
                    provinceCode = code;
                    tvProvienceName.setText(provinceName);

                    cityName = "";
                    cityCode = "";
                    tvCityName.setText("市");

                    areaName = "";
                    areaCode = "";
                    tvAreaName.setText("区/县");

                } else if (level == 2) {

                    cityName = name;
                    cityCode = code;

                    tvCityName.setText(cityName);

                    areaName = "";
                    areaCode = "";
                    tvAreaName.setText("区/县");

                } else if (level == 3) {
                    areaName = name;
                    areaCode = code;
                    tvAreaName.setText(areaName);
                }

                level++;

                if (level == 4) {

                    onCallBackListener.onCallBack(provinceName, provinceCode, cityName, cityCode, areaName, areaCode);

                    dismiss();
                } else {
                    requestGetRegion(Integer.valueOf(code), level);
                }

                setDisplay(level);


                Log.e("address--name", name);
                Log.e("address--code", code);
                Log.e("address--level", String.valueOf(level));


            }
        });

    }


    public static AddressDialog newInstance(String type) {
        Bundle args = new Bundle();
        AddressDialog fragment = new AddressDialog();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    private void initView() {

    }


    public void setDisplay(int type) {

        viewProvience.setVisibility(View.INVISIBLE);
        viewCity.setVisibility(View.INVISIBLE);
        viewArea.setVisibility(View.INVISIBLE);


        if (type == 1) {
            viewProvience.setVisibility(View.VISIBLE);
        }
        if (type == 2) {
            viewCity.setVisibility(View.VISIBLE);
        }
        if (type == 3) {
            viewArea.setVisibility(View.VISIBLE);
        }


    }

    public void requestGetRegion(int code, int level) {

        homePageModel.requestGetRegion(code, level, new DataCallBack<BankDeposiAddressListBean>() {
            @Override
            public void onSuccessful(BankDeposiAddressListBean bean) {

                if (bean.getData().size() == 0) {

                    onCallBackListener.onCallBack(provinceName, provinceCode, cityName, cityCode, areaName, areaCode);

                    dismiss();
                }

                Log.e("bean=", new Gson().toJson(bean));

                addressList.clear();
                addressList.addAll(bean.getData());
                provienceAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.ll_provience, R.id.ll_city, R.id.ll_area})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_provience:
                setDisplay(1);
                level = 1;
                requestGetRegion(0000, level);
                break;
            case R.id.ll_city:

                if ("".equals(provinceName)) {
                    Toast.makeText(mActivity, "请选择省", Toast.LENGTH_SHORT).show();
                    return;
                }
                setDisplay(2);
                level = 2;
                requestGetRegion(Integer.valueOf(provinceCode), level);
                break;
            case R.id.ll_area:
                if ("".equals(cityName)) {
                    Toast.makeText(mActivity, "请选择市", Toast.LENGTH_SHORT).show();
                    return;
                }
                setDisplay(3);
                level = 3;
                requestGetRegion(Integer.valueOf(cityCode), level);
                break;
        }
    }

    public interface OnCallBackListener {
        void onCallBack(String provinceName, String provinceCode, String cityName, String cityCode, String areaName, String areaCode);
    }
}
